package cn.mystyle.controller.account;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.mystyle.common.bean.BaseController;
import cn.mystyle.controller.user.UserController;
import cn.mystyle.model.account.Record;
import cn.mystyle.model.user.UserInfo;
import cn.mystyle.service.account.RecordService;
import cn.mystyle.util.UserUtil;

/**
 * @author clark
 * 
 * 记账控制器
 */
@RestController
@RequestMapping(value = "/account/record")
public class RecordController extends BaseController
{
    /**
     * 日志记录
     */
    private Logger logger = LoggerFactory.getLogger(UserController.class);
    
    /**
     * 记账业务类
     */
    @Autowired
    private RecordService recordService;
    
    /**
     * 记账
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param paramData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        logger.info("add record, param:{}", jsonData);
        
        // 参数转换
        Record recordParam = JSON.parseObject(jsonData, Record.class);
        
        // 获取当前登录用户
        UserInfo sessionUser = UserUtil.seesionUser(request);
        
        return recordService.addRecord(sessionUser, recordParam).toString();
    }
    
    /**
     * 查询账目
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param paramData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public String queryRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        logger.info("query record, param:{}", jsonData);
        
        // 参数转换
        Record recordParam = JSON.parseObject(jsonData, Record.class);
        
        // 获取当前登录用户
        UserInfo sessionUser = UserUtil.seesionUser(request);
        
        return recordService.queryRecord(sessionUser, recordParam).toString();
    }
    
    /**
     * 删除
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param paramData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public String delRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        logger.info("delete record, param:{}", jsonData);
        
        // 参数转换
        Record recordParam = JSON.parseObject(jsonData, Record.class);
        
        // 获取当前登录用户
        UserInfo sessionUser = UserUtil.seesionUser(request);
        
        return recordService.delRecord(sessionUser, recordParam).toString();
    }
    
    /**
     * 更新
     * @param request HTTP 请求体
     * @param response HTTP 响应体
     * @param paramData 请求参数
     * @return String  
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String updateRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody String jsonData)
    {
        logger.info("update record, param:{}", jsonData);
        
        // 参数转换
        Record recordParam = JSON.parseObject(jsonData, Record.class);
        
        // 获取当前登录用户
        UserInfo sessionUser = UserUtil.seesionUser(request);
        
        return recordService.updateRecord(sessionUser, recordParam).toString();
    }
}
